{
 "metadata": {
  "name": "",
  "signature": "sha256:8d9fb5fd6d45ac72e2e8d74a516cc937e8081ebf05f5f35ba577becf25b18631"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#\u8bad\u7ec3\u96c6\u5bf9\u5e94\u7684label\n",
      "label = [0, 0, 0, 0, 1, 1, 1, 1, 1]\n",
      "#\u8bad\u7ec3\u96c65\u4e2a\u7279\u5f81\n",
      "train = [[0, 0, 30, 450, 7],\n",
      "             [1, 1, 5, 500, 3],\n",
      "             [1, 0, 10, 150, 1],\n",
      "             [0, 1, 40, 300, 6],\n",
      "             [1, 0, 20, 100, 10],\n",
      "             [0, 1, 25, 180, 12],\n",
      "             [0, 0, 32, 50, 11],\n",
      "             [1, 0, 23, 120, 9],\n",
      "             [0, 0, 27, 200, 8]]\n",
      "#\u76ee\u6807\uff1a\u6839\u636e\u6d4b\u8bd5\u96c6\u7ed9\u51fa\u76845\u4e2a\u7279\u5f81\uff0c\u5224\u65ad\u6d4b\u8bd5\u96c6\u5bf9\u5e94\u7684label\n",
      "test = [0, 0, 40, 180, 8]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 29
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def data_process(train, test, indexlist):\n",
      "    for index in indexlist:\n",
      "        temp = []\n",
      "        for i in range(len(train)):\n",
      "            temp.append(train[i][index])  #\u4f9d\u6b21\u53d6\u51fa[2,3,4]\u5217\u4fdd\u5b58\u5728\u4e34\u65f6list\u4e2d\uff0c\u7528\u6765\u5bf92\uff0c3\uff0c4\u5217\u8fdb\u884c\u5f52\u4e00\u5316\n",
      "        tmax, tmin = max(temp), min(temp) #\u6c42\u51faindex\u5217\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\n",
      "        p1 = tmin + ((tmax - tmin) / 3)   #\u603b\u5171\u5bf9\u8be5\u6570\u636e\u5206\u62103\u6bb5\uff1a0\uff0c1\uff0c2  p1\u4e3a0\uff0c1\u7684\u5206\u5272\n",
      "        p2 = tmax - (tmax - tmin) / 3     #p1\u4e3a1\uff0c2\u7684\u5206\u5272\n",
      "        #\u5bf9\u8bad\u7ec3\u96c6\u5bf9\u5e94\u5217\u7684\u6570\u636e\u8fdb\u884c\u5206\u6bb5\n",
      "        for i in range(len(temp)):\n",
      "            if temp[i] <= p1:\n",
      "                train[i][index] = 0\n",
      "            elif temp[i] <= p2:\n",
      "                train[i][index] = 1\n",
      "            else:\n",
      "                train[i][index] = 2\n",
      "        # \u5bf9\u6d4b\u8bd5\u96c6\u5bf9\u5e94\u5217\u7684\u6570\u636e\u8fdb\u884c\u5206\u6bb5\n",
      "        if test[index] <= p1:\n",
      "            test[index] = 0\n",
      "        elif test[index] <= p2:\n",
      "            test[index] = 1\n",
      "        else:\n",
      "            test[index] = 2\n",
      "    return train, test"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 30
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "train, test = data_process(train, test, [2, 3, 4])\n",
      "print (train)\n",
      "print (test)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[[0, 0, 2, 2, 1], [1, 1, 0, 2, 0], [1, 0, 0, 0, 0], [0, 1, 2, 1, 1], [1, 0, 1, 0, 2], [0, 1, 1, 0, 2], [0, 0, 2, 0, 2], [1, 0, 1, 0, 2], [0, 0, 1, 0, 1]]\n",
        "[0, 0, 2, 0, 1]\n"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "feature_num = len(test)  #\u7279\u5f81\u4e2a\u6570\n",
      "sample_num = len(train)  #\u8bad\u7ec3\u96c6\u6837\u672c\u4e2a\u6570\n",
      "prior = [[0]*3 for _ in range(feature_num)]  #\u5148\u9a8c\u6982\u7387\n",
      " #\u6c42\u5404\u4e2a\u7279\u5f81\u5148\u9a8c\u7684\u6982\u7387\n",
      "for i in range(feature_num):\n",
      "    temp = []\n",
      "    for j in range(sample_num):\n",
      "        temp.append(train[j][i])\n",
      "    for j in range(3):\n",
      "        prior[i][j] = temp.count(j) / sample_num\n",
      "        \n",
      "label0 = []\n",
      "label1 = []\n",
      "for i in range(len(label)):\n",
      "    if label[i] == 0:\n",
      "        label0.append(i)\n",
      "    else:\n",
      "        label1.append(i)\n",
      "        \n",
      "condition = [[[0]*3 for _ in range(feature_num)] for _ in range(2)]#\u6761\u4ef6\u6982\u7387\n",
      "#\u6839\u636e\u6b63\u8d1f\u6837\u672c\u6c42\u6761\u4ef6\u6982\u7387\uff0c\u540e\u9a8c\u6982\u7387\n",
      "for i in range(feature_num):\n",
      "    temp = []\n",
      "    for j in label0:\n",
      "        temp.append(train[j][i])\n",
      "    for j in range(3):\n",
      "        condition[0][i][j] = temp.count(j) / len(label0)\n",
      "    temp = []\n",
      "    for j in label1:\n",
      "        temp.append(train[j][i])\n",
      "    for j in range(3):\n",
      "        condition[1][i][j] = temp.count(j) / len(label1)\n",
      "#\u6c42\u6b63\u8d1f\u6837\u672c\u7684\u5148\u9a8c\u6982\u7387\n",
      "l0, l1 = len(label0)/sample_num, len(label1)/sample_num\n",
      "res1 = l1\n",
      "res0 = l0\n",
      "\n",
      "#\u5206\u522b\u6c42\u5f52\u4e3a\u6b63\u6837\u672c\u7684\u6982\u7387res1\uff0c\u5f52\u4e3a\u8d1f\u6837\u672c\u7684\u6982\u7387res2\n",
      "for i in range(feature_num):\n",
      "    res1 *= condition[1][i][test[i]]  # \u6839\u636e\u5404\u7279\u5f81\u72ec\u7acb\uff0c\u6c42\u540c\u65f6\u53d1\u751f\u7684\u6982\u7387\n",
      "    res1 /= prior[i][test[i]]  #\u6c42\u7684\u8be5\u7279\u5f81\u4e0b\u4e3a\u6b63\u6837\u672c\u7684\u6982\u7387\uff08\u6761\u4ef6\u6982\u7387\uff09\n",
      "for i in range(feature_num):\n",
      "    res0 *= condition[0][i][test[i]]  # \u6839\u636e\u5404\u7279\u5f81\u72ec\u7acb\uff0c\u6c42\u540c\u65f6\u53d1\u751f\u7684\u6982\u7387\n",
      "    res0 /= prior[i][test[i]]  #\u6c42\u7684\u8be5\u7279\u5f81\u4e0b\u4e3a\u8d1f\u6837\u672c\u7684\u6982\u7387\uff08\u6761\u4ef6\u6982\u7387\uff09\n",
      "print (res1)\n",
      "print (res0)\n",
      "print (round(res1/res0, 3)) #\u5927\u4e8e 1 \u5f52\u4e3a\u6b63\u6837\u672c\uff0c\u5c0f\u4e8e 1 \u5f52\u4e3a\u8d1f\u6837\u672c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0.3888000000000001\n",
        "0.25312500000000004\n",
        "1.536\n"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}